import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class MyListener implements MessageListener {
    private double msg;
    private double sum = 0;
    private int count = 0;

    private double max = -99999;
    private double min = 9999;
    private double sum_me = 0;
    private double average;

    private double me;
    private int N;
    private int epoch;

    private double[] msglist = new double[999];

    public void Nequal(int N){
        this.N = N;
    }


    public void onMessage(Message message) {
        String brokerURL = "tcp://localhost:61616";
        ConnectionFactory factory = null;
        Connection connection = null;
        Session session = null;
        Topic topic = null;
        MessageProducer messageProducer = null;
        Object[] message_list = new Object[999];
        try {
            Singnal_Anasis a = new Singnal_Anasis();
            try {
                factory = new ActiveMQConnectionFactory(brokerURL);
                connection = factory.createConnection();
                connection.start();
                session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                topic = session.createTopic("Random_plot_package");
                messageProducer = session.createProducer(topic);
            } catch (Exception e){
                e.printStackTrace();
            }
            if (count < N) {
//              System.out.println(N);
//              System.out.println(count);
//                System.out.println(msglist.length);
                msg = (double)(((ObjectMessage) message).getObject());
//                System.out.println("已经存储" + message_list[count]);
                msglist[count] = msg;
//                assert messageProducer != null;
                messageProducer.send(message);
                System.out.println("已接受到的随机分布数 == "+msg);

//                System.out.println(msglist[count]);
                if (msg > max) max = msg;
                if (msg < min) min = msg;
                sum = sum + msg;
                count++;
//                System.out.println(count);
            } else {
                average = sum / N;
                for (int i = 0; i < N; i++) {
                    sum_me += (msglist[i] - average) * (msglist[i] - average);
                }
                me = sum_me / N;
                epoch++;
                System.out.println("以上" + N + "个为即将打包的数据,本批次为第"+epoch+"批数据");
//                System.out.println("方差 == "+me);
//                System.out.println("均值 = " +sum);
                try{
                    assert session != null;
                    message_list[N] = session.createObjectMessage(average);
                    message_list[N+1] = session.createObjectMessage(me);
                    message_list[N+2] = session.createObjectMessage(max);
                    message_list[N+3] = session.createObjectMessage(min);
//                    all_message[4] = getMsglist();

//                    System.out.println(message_list);
//                    System.out.println(message_list[0]);
                    assert messageProducer != null;
                    messageProducer.send((Message) message_list[N]);
                    messageProducer.send((Message) message_list[N+1]);
                    messageProducer.send((Message) message_list[N+2]);
                    messageProducer.send((Message) message_list[N+3]);
                    System.out.println("average: "+average);
                    System.out.println("me: "+me);
                    System.out.println("max: "+max);
                    System.out.println("min: "+min);
                } catch (Exception e){
                    e.printStackTrace();
                }
                count = 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    public double getave(){
//        return this.average;
//    }
//
//    public double getme(){
//        return this.me;
//    }
//
//    public double getmax(){
//        return this.max;
//    }
//
//    public double getmin(){
//        return this.min;
//    }
//
//    public double getcount(){
//        return this.count;
//    }
//
//    public double[] getMsglist(){
//        return this.msglist;
//    }
}